﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VSDBM.Code.dao;
using System.IO;
using System.Text;

public partial class Buscadoreg : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                String id = Session["idreg"].ToString();
                String acession = Session["idacession"].ToString();
                DataSet ds = SequenceDAO.selectbuscador(" WHERE s.id LIKE '" + id + "' AND s.idacession LIKE '"+acession+"'");
                DataTable dt = ds.Tables[0];
                DataTable dttotal = SequenceDAO.getReferenceTotalSize(int.Parse(dt.Rows[0]["idfeature"].ToString())).Tables[0];
                String content = "";
                content += "<pre><tt><font size=+1>";
                content += "       REGION MAP:     " + GenBankFile.geramapa(int.Parse(dttotal.Rows[0][0].ToString()), int.Parse(dt.Rows[0]["positioninitial"].ToString()), int.Parse(dt.Rows[dt.Rows.Count-1]["size"].ToString()));
                content += "            LOCUS:     " + GenBankFile.formataCampoComum(dt.Rows[0]["locus"].ToString()) + "<br />";
                content += "         ACESSION:     " + GenBankFile.formataCampoComum(dt.Rows[0]["idacession"].ToString()) + "<br />";
                content += "         ORGANISM:     " + GenBankFile.formataCampoComum(dt.Rows[0]["name"].ToString()) + "<br />";
                content += "       DEFINITION:     " + GenBankFile.formataCampoComum(dt.Rows[0]["definition"].ToString()) + "<br />";
                content += "             SIZE:     " + GenBankFile.formataCampoComum(dt.Rows[0]["size"].ToString()) + "<br />";
                content += "          SUBTYPE:     " + GenBankFile.formataCampoComum(dt.Rows[0]["description"].ToString()) + "<br />";
                content += "          FEATURE:     " + GenBankFile.formataCampoComum(dt.Rows[0]["featuremap"].ToString()) + "<br />";
                content += "        QUALIFIER:     " + GenBankFile.formataCampoComum(dt.Rows[0]["qualifier"].ToString()) + "<br />";
                content += "         SEQUENCE:     " + GenBankFile.formataCampoComum(dt.Rows[0]["sequence"].ToString()) + "<br />";
                content += "</font></tt></pre>";
                genbankcontent.Text = content;

                lbtitulo.Text = (dt.Rows[0]["idacession"].ToString());

            }
            catch (Exception ex)
            {
                System.Console.Write(ex.Message);
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        String acession = Session["idreg"].ToString();
        DataSet ds = SequenceDAO.selectbuscador(" WHERE idacession LIKE '" + acession + "'");
        DataTable dt = ds.Tables[0];

        String content = "";
        content += "            LOCUS:     " + GenBankFile.formataCampoComumTXT(dt.Rows[0]["locus"].ToString()) + "\r\n";
        content += "         ACESSION:     " + GenBankFile.formataCampoComumTXT(dt.Rows[0]["idacession"].ToString()) + "\r\n";
        content += "         ORGANISM:     " + GenBankFile.formataCampoComumTXT(dt.Rows[0]["name"].ToString()) + "\r\n";
        content += "       DEFINITION:     " + GenBankFile.formataCampoComumTXT(dt.Rows[0]["definition"].ToString()) + "\r\n";
        content += "             SIZE:     " + GenBankFile.formataCampoComumTXT(dt.Rows[0]["size"].ToString()) + "\r\n";
        content += "          SUBTYPE:     " + GenBankFile.formataCampoComumTXT(dt.Rows[0]["description"].ToString()) + "\r\n";
        content += "          FEATURE:     " + GenBankFile.formataCampoComumTXT(dt.Rows[0]["featuremap"].ToString()) + "\r\n";
        content += "        QUALIFIER:     " + GenBankFile.formataCampoComumTXT(dt.Rows[0]["qualifier"].ToString()) + "\r\n";
        content += "         SEQUENCE:     " + GenBankFile.formataCampoComumTXT(dt.Rows[0]["sequence"].ToString()) + "\r\n";


        String IdList = content;
        String FileName = dt.Rows[0]["idacession"].ToString();
        Response.ContentType = "text/plain";
        Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName + ".txt");
        Response.Write(IdList);
        Response.End();
    }
    protected void btgetfasta_Click(object sender, EventArgs e)
    {
        String acession = Session["idreg"].ToString();
        DataSet ds = SequenceDAO.selectbuscador(" WHERE idacession LIKE '" + acession + "'");
        DataTable dt = ds.Tables[0];

        String content = "";
        content += ">" +dt.Rows[0]["idacession"].ToString();
        content += " | " + dt.Rows[0]["name"].ToString();
        content += " | " + dt.Rows[0]["definition"].ToString();
        content += "\r\n" + GenBankFile.formataCampoComumTXTsemEspacos(dt.Rows[0]["sequence"].ToString()) + "\r\n";


        String IdList = content;
        String FileName = dt.Rows[0]["idacession"].ToString();
        Response.ContentType = "text/plain";
        Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName + ".txt");
        Response.Write(IdList);
        Response.End();
    }
}